package dispinfo;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Graphics;

public abstract class Provider
{
	abstract public void requestData();
	abstract public int getRequestDataPeriod();
	abstract public void paint(Graphics g, int width, int height);
	abstract public int getHeight();
	private TimerTask prevTask;
	
	public void asynchRequestData()
	{
		new Thread() 
		{
			public void run()
			{
				try
				{
					requestData();
					DipsInfoMidlet.onProviderNewData(Provider.this);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}.start();
	}
	
	public void run()
	{
		asynchRequestData();
	}
	
	public void startRequestTimer(Timer timer)
	{
		if (prevTask != null) prevTask.cancel();
		prevTask = new TimerTask()
		{
			public void run()
			{
				asynchRequestData();
			}
		};
		timer.schedule(prevTask, 0, 1000 * getRequestDataPeriod());
	}
}
